/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 *
 * 			http://www.kkkd.com/
 *
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Address;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.NewOrder;
import com.simon.mengkou.data.bean.base.Product;
import com.simon.mengkou.data.bean.base.Store;
import com.simon.mengkou.data.cache.DataCacheImpl;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.adapter.OrderNewConfirmAdapter;
import com.simon.mengkou.utils.UtilOuer;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/4/13.
 * desc    : 确认订单界面
 */
public class OrderNewConfirmActivity extends BaseTopActivity{
    @Bind(R.id.order_detail_id_list)
    ListView mLvList;

    //地址
    TextView mTvAddress;
    //姓名
    TextView mTvName;

    //商品总价
    TextView mTvAmount;
    //折扣
    TextView mTvDiscount;
    //运费
    TextView mTvFreight;
    //总计
    @Bind(R.id.cart_id_total)
    TextView mTvTotal;

    private OrderNewConfirmAdapter mAdapter;
    private NewOrder mOrder;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mOrder = (NewOrder) getIntent().getSerializableExtra(CstOuer.KEY.ORDER);
    }

    @Override
    protected void initTop() {
        setTitle(R.string.order_confirm_title);
        setNavigation(R.drawable.common_ic_left_nav);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_order_confirm);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        //头部
        View header = LayoutInflater.from(this).inflate(R.layout.layout_order_confirm_header, null);
        mTvAddress = (TextView)header.findViewById(R.id.order_detail_id_address);
        mTvName = (TextView)header.findViewById(R.id.order_detail_id_name);
        mLvList.addHeaderView(header);

        header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OuerDispatcher.presentAddressManagerActivity(OrderNewConfirmActivity.this);
            }
        });

        //尾部
        View footer = LayoutInflater.from(this).inflate(R.layout.layout_order_confirm_detail_footer, null);
        mTvAmount = (TextView)footer.findViewById(R.id.order_detail_id_discount);
        mTvFreight = (TextView)footer.findViewById(R.id.order_detail_id_freight);
        mTvDiscount = (TextView)footer.findViewById(R.id.order_detail_id_coupon);
        mLvList.addFooterView(footer);


        footer.findViewById(R.id.order_detail_id_coupon_root).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OuerDispatcher.presentOrderCouponActivity(OrderNewConfirmActivity.this);
            }
        });

        mAdapter = new OrderNewConfirmAdapter(this);
        mLvList.setAdapter(mAdapter);

        setOrder();

        registerAction(CstOuer.BROADCAST_ACTION.ORDER_COUPON_PRESUBMIT_ACTION);
    }


    @Override
    protected void onReceive(Intent intent) {
        super.onReceive(intent);
        if(CstOuer.BROADCAST_ACTION.ORDER_COUPON_PRESUBMIT_ACTION.equals(intent.getAction())) {
            mOrder = (NewOrder) intent.getSerializableExtra(CstOuer.KEY.ORDER);
            //优惠
            mTvDiscount.setText(getString(R.string.common_price,
                    UtilOuer.formatPrice(mOrder.getDiscount())));
            //总计
            mTvTotal.setText(Html.fromHtml(getString(R.string.cart_total,
                    UtilOuer.formatPrice(mOrder.getAmount()))));
        }
    }

    private void setOrder() {
        if(mOrder == null) {
            return;
        }


        //商品总价
        mTvAmount.setText(getString(R.string.common_price,
                UtilOuer.formatPrice(mOrder.getTotalAmount())));
        //运费
        mTvFreight.setText(getString(R.string.common_price,
                UtilOuer.formatPrice(mOrder.getFreight())));

        //总计
        mTvTotal.setText(Html.fromHtml(getString(R.string.cart_total,
                UtilOuer.formatPrice(mOrder.getAmount()))));

        //商品列表
        mAdapter.setList(mOrder.getStoreList());

        //
    }


    @Override
    protected void onResume() {
        super.onResume();
        setAddress();
    }

    private void setAddress() {
        Address addr = OuerApplication.mCacheFactory.getCache(DataCacheImpl.AddressDefaultCache.class,
                OuerApplication.mUser.getUserId()).getData();

        if(addr == null) {
            addr = mOrder.getAddress();
        }

        if(addr != null) {
            //地址
            mTvAddress.setText(getString(R.string.my_order_detail_address,
                    addr.getAddress() + addr.getDetail()));
            //姓名
            mTvName.setText(getString(R.string.my_order_detail_name, addr.getName(),
                    UtilString.nullToEmpty(addr.getPhone())));
        }
    }

    /**
     * 确认
     */
    @OnClick(R.id.order_detail_id_delete)
    public void confirmOrder() {
        //收货地址空
        if(UtilString.isBlank(mTvAddress.getText().toString())) {
            UtilOuer.toast(this, R.string.order_confirm_addr);
            return;
        }

        Address addr = OuerApplication.mCacheFactory.getCache(DataCacheImpl.AddressDefaultCache.class,
                OuerApplication.mUser.getUserId()).getData();

        if(addr == null) {
            addr = mOrder.getAddress();
        }

        AgnettyFuture future = OuerApplication.mOuerFuture.submitOrder(addr.getId(), null,
                new OuerFutureListener(this) {
                    @Override
                    public void onStart(AgnettyResult result) {
                        super.onStart(result);
                        setWaitingDialog(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setWaitingDialog(false);
                        NewOrder order = (NewOrder) result.getAttach();
                        OuerDispatcher.presentOrderDetailActivity(OrderNewConfirmActivity.this,
                                order.getId(), null);
                        getCartCount();
                        finish();
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setWaitingDialog(false);
                        UtilOuer.toast(OrderNewConfirmActivity.this, R.string.order_confirm_confirm_failure);
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setWaitingDialog(false);
                    }
                });

        attachDestroyFutures(future);

    }


    private void getCartCount() {
        AgnettyFuture future = OuerApplication.mOuerFuture.cartProductNum(null);
        attachDestroyFutures(future);
    }

}
